<template>
  <el-dialog title="修改密码" :visible.sync="open" width="350px">
    <el-form
      ref="formData"
      :model="formData"
      :rules="rules"
      size="small"
      label-width="80px"
    >
      <el-form-item prop="oldpwd" label="旧密码">
        <el-input
          v-model="formData.oldpwd"
          show-password
          placeholder="旧密码"
        ></el-input>
      </el-form-item>
      <el-form-item prop="pwd" label="新密码">
        <el-input
          v-model="formData.pwd"
          show-password
          placeholder="新密码"
        ></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button
        type="primary"
        @click="onSubmit('formData')"
        size="small"
        :loading="loading"
        >确定</el-button
      >
    </div>
  </el-dialog>
</template>
<script>
import { updateMpwd } from "@/api/userManagement/user";
import md5 from "js-md5";
export default {
  data() {
    return {
      open: false,
      loading: false,
      roleList: [],
      formData: {
        oldpwd: "",
        pwd: "",
      },
      rules: {
        oldpwd: [{ required: true, message: "请输入旧密码", trigger: "blur" }],
        pwd: [{ required: true, message: "请输入新密码", trigger: "blur" }],
      },
    };
  },
  methods: {
    show() {
      this.open = true;
    },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          var formData = {
            mgrSn: this.$store.getters.mgrSn,
            oldpwd: md5(this.formData.oldpwd),
            pwd: md5(this.formData.pwd),
          };
          updateMpwd(formData).then((res) => {
            this.loading = false;
            if (res.data.resultStatus) {
              this.open = false;
              this.$message.success(res.data.errorMessage);
              this.$emit("load");
            }
          });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>